<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="员工编号" prop="empNum">
      <el-input v-model="dataForm.empNum" placeholder="员工编号"></el-input>
    </el-form-item>
    <el-form-item label="员工姓名" prop="empName">
      <el-input v-model="dataForm.empName" placeholder="员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="联系传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="联系传真"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="zip">
      <el-input v-model="dataForm.zip" placeholder="邮编"></el-input>
    </el-form-item>
    <el-form-item label="联系地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
    </el-form-item>
    <el-form-item label="用户序号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户序号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          empNum: '',
          empName: '',
          sex: '',
          phone: '',
          fax: '',
          email: '',
          zip: '',
          address: '',
          userId: ''
        },
        dataRule: {
          empNum: [
            { required: true, message: '员工编号不能为空', trigger: 'blur' }
          ],
          empName: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '联系传真不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          zip: [
            { required: true, message: '邮编不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '联系地址不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户序号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/employee/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.empNum = data.employee.empNum
                this.dataForm.empName = data.employee.empName
                this.dataForm.sex = data.employee.sex
                this.dataForm.phone = data.employee.phone
                this.dataForm.fax = data.employee.fax
                this.dataForm.email = data.employee.email
                this.dataForm.zip = data.employee.zip
                this.dataForm.address = data.employee.address
                this.dataForm.userId = data.employee.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/employee/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'empNum': this.dataForm.empNum,
                'empName': this.dataForm.empName,
                'sex': this.dataForm.sex,
                'phone': this.dataForm.phone,
                'fax': this.dataForm.fax,
                'email': this.dataForm.email,
                'zip': this.dataForm.zip,
                'address': this.dataForm.address,
                'userId': this.dataForm.userId
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
